package com.beamfield.controller.manage.steelplant;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.Steelplant;
import com.beamfield.modelVo.FileInfoVo;
import com.beamfield.service.manage.lab.LabService;
import com.beamfield.service.manage.steelplant.SteelplantBuildService;
import com.beamfield.service.manage.sys.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 钢筋加工厂建设
 */
@Controller
@RequestMapping(value = "/cms/rebar/source-mill")
public class SteelplantBuildController extends BaseController {

    private final SteelplantBuildService steelplantBuildService;

    @Autowired
    public SteelplantBuildController(SteelplantBuildService steelplantBuildService) {
        this.steelplantBuildService = steelplantBuildService;
    }

    @Autowired
    private LabService labService;

    @Value("${session.beam}")
    private String beam;

    @Value("${session.user}")
    private String user;


    @RequestMapping(value = "/organization")
    public ModelAndView oranization() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/institution")
    public ModelAndView institution() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/operator")
    public ModelAndView operator() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/adjustment")
    public ModelAndView adjustment() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/editInstitution")
    public ModelAndView editInstitution() {
        return new ModelAndView();
    }

    /**
     * 获取钢筋加工厂数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/readSteelplantData", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readSteelplantData(HttpServletRequest request) {
        Long beamId = super.getBeamId(request);
        Steelplant steelplantData = steelplantBuildService.getSteelplantData(beamId);
        return new ApiResult(steelplantData == null ? "" : steelplantData);
    }


    @RequestMapping(value = "/editManagerInfo", method = RequestMethod.GET)
    public ModelAndView editManagerInfo() {
        return new ModelAndView();
    }

    /**
     * 获取管理人员列表
     *
     * @param offset
     * @param limit
     * @param name
     * @param identity
     * @return
     */
    @RequestMapping("/getSteelplantManagerList")
    @ResponseBody
    public PageResult getSteelplantManagerList(Integer offset, Integer limit, String identity, String name, HttpSession session, String presasg) {
        Long beam_id = (Long) session.getAttribute(beam);
        return labService.getManagerInfoList(offset, limit, identity, name, beam_id, "990110", null, presasg);
    }

    /**
     * 删除钢筋加工厂管理人员
     *
     * @return
     */
    @RequestMapping("/delSteelplantManager")
    @ResponseBody
    public ApiResult delSteelplantManager(String id) {
        if (StringUtils.isNotBlank(id)) {
            return steelplantBuildService.delSteelplantManager(id);
        } else {
            return new ApiResult("500", "未获取删除人员");
        }
    }


    @RequestMapping(value = "/constr_inspection")
    public ModelAndView constr_inspection() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/in_acceptance")
    public ModelAndView in_acceptance() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/sup_acceptance")
    public ModelAndView sup_acceptance() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/owner_acceptance")
    public ModelAndView owner_acceptance() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/data_archive")
    public ModelAndView data_archive() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/comm_acceptance")
    public ModelAndView comm_acceptance() {
        return new ModelAndView();
    }

    @Autowired
    FileService fileService;

    /**
     * 获取文件
     *
     * @return
     */
    @RequestMapping("/getFileInfoVo")
    @ResponseBody
    public FileInfoVo getFileInfoVo(Long file_id) {
        return fileService.getFileInfoVo(file_id);
    }

    /**
     * 获取归档文件列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getDataArchiveList", method = RequestMethod.GET)
    @ResponseBody
    public PageResult getDataArchiveList(HttpServletRequest request) {
        Long beamId = super.getBeamId(request);
        return steelplantBuildService.getDataArchiveList(beamId);
    }

    /**
     * 删除归档资料
     *
     * @return
     */
    @RequestMapping("/delDataArchive")
    @ResponseBody
    public ApiResult delDataArchive(String type, HttpServletRequest request) {
        Long beamId = super.getBeamId(request);

        if (StringUtils.isNotBlank(type)) {
            return steelplantBuildService.delDataArchive(beamId, type);
        } else {
            return new ApiResult("500", "未获取删除的对应文件");
        }
    }

    /**
     * 保存钢筋加工厂
     */
    @RequestMapping(value = "/saveSteelplant", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveSteelplant(@RequestParam(value = "result", required = false) String sresult, HttpServletRequest request,
                                    @RequestParam(value = "file", required = false) MultipartFile multipartFile,
                                    @RequestParam(value = "type") String type, String institution) {
        if (StringUtils.isEmpty(type)) {
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        int result = 0;
        try {
            Long fileId = null;
            if (multipartFile != null) {
                fileId = super.saveFile((CommonsMultipartFile) multipartFile);
            }
            result = steelplantBuildService.saveSteelplant(userId, beamId, sresult, fileId, type, institution);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }
    }

    /*
    更新钢筋加工厂信息
     */
    @RequestMapping(value = "/saveInstitutionFile", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveInstitutionFile(@RequestParam(value = "result", required = false) String sresult, HttpServletRequest request,
                                         @RequestParam(value = "file", required = false) MultipartFile multipartFile,
                                         @RequestParam(value = "type") String type, String institution) {
        if (StringUtils.isEmpty(type)) {
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        List<String> fileNames = new ArrayList<>();
        String fileIds = null;
        int result = 0;
        try {
            Long fileId = null;
            if (multipartFile != null) {
                fileId = super.saveFile((CommonsMultipartFile) multipartFile);
                fileNames.add(fileId.toString());
            }
            fileIds = listToString(fileNames);
            result = steelplantBuildService.saveInstitutionFile(userId, beamId, sresult, fileIds, type, institution);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }
    }

    @RequestMapping(value ="/updateInstitutionFile",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateInstitutionFile( HttpServletRequest request, String institution_file){
        System.out.println(institution_file);
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        int result = 0;
        try {
            steelplantBuildService.updateInstitutionFile(userId, beamId, institution_file);
        }catch(Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());

        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }

    }


    public static String listToString(List<String> stringList) {

        if (stringList == null) {

            return null;

        }

        StringBuilder result = new StringBuilder();

        boolean flag = false;

        for (String string : stringList) {

            if (flag) {

                result.append(",");

            } else {

                flag = true;

            }

            result.append(string);

        }

        return result.toString();

    }


}
